﻿
window.onload=function(){
	//得到当前页面所有的grid对象
	/*var grids=$("table[class='easyui-datagrid']");
	for(var i=0;i<grids.length;i++){
		//只处理有值的
		if("undefined" != typeof $(grids[i]).attr("noparse")){
			//加了noparse 属性的不进行渲染
			continue;
		}
		if("undefined" != typeof $(grids[i]).attr("id")){	
			inital_grid_pagination($(grids[i]).attr("id"));
		}
		
	}*/
	
}
/**
 * 序列化成json字符串
 * @param obj
 * @return
 */
function Serialize(obj){     
    switch(obj.constructor){     
        case Object:     
            var str = "{";     
            for(var o in obj){     
                str += o + ":" + Serialize(obj[o]) +",";     
            }     
            if(str.substr(str.length-1) == ",")     
                str = str.substr(0,str.length -1);     
             return str + "}";     
             break;     
         case Array:                 
             var str = "[";     
             for(var o in obj){     
                 str += Serialize(obj[o]) +",";     
             }     
             if(str.substr(str.length-1) == ",")     
                 str = str.substr(0,str.length -1);     
             return str + "]";     
             break;     
         case Boolean:     
             return "\"" + obj.toString() + "\"";     
             break;     
         case Date:     
             return "\"" + obj.toString() + "\"";     
             break;     
         case Function:     
             break;     
         case Number:     
             return "\"" + obj.toString() + "\"";     
             break;      
         case String:     
             return "\"" + obj.toString() + "\"";     
             break;         
     }     
 } 
/**
 * 初始化grid的分页栏
 * @param tablename  grid id
 * @return	
 */
function inital_grid_pagination(tablename){
	//begin
	$('#'+tablename).datagrid('getPager').pagination({  
	    beforePageText: '第',
	    afterPageText: '页    共 {pages} 页',   
	    displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录 ',
	    layout:['list','sep','first','prev','links','next','last','sep','refresh','manual']/*,
	    onSelectPage:function(pageNumber, pageSize){//跳转页面前调用
	    	$('#'+tablename).datagrid('options').pageNumber=pageNumber;
	    	$('#'+tablename).datagrid('options').pageSize=pageSize;	    	
	    	$('#'+tablename).datagrid('reload',$('#'+tablename).datagrid('options').queryParams);
		}*/
	});
	//end
		

}


/**
 * 根据列标题返回字段名
 * @param ftitle
 * @param tablename
 * @return
 */
function getFieldsName(ftitle,tablename){	
	var filedsname="";
	var fields = $('#'+tablename).datagrid('options').columns;
		for(var i=0; i<fields[0].length; i++){
			if(fields[0][i].title==ftitle){
			filedsname=fields[0][i].field;
			break;
			}
		}
		return filedsname;
	}
/**
 * 创建grid 列的右键菜单
 * @param tablename
 * @return
 */
function createColumnMenu(tablename){
	var tmenu = $('<div id="tmenu" style="width:100px;"></div>').appendTo('body');
	var fields = $('#'+tablename).datagrid('options').columns;
	for(var i=0; i<fields[0].length; i++){
		if(!fields[0][i].checkbox){
			$('<div iconCls="icon-ok"/>').html(fields[0][i].title).appendTo(tmenu);
		}		
		
	}
	tmenu.menu({			
		onClick: function(item){
			if (item.iconCls=='icon-ok'){
				$('#'+tablename).datagrid('hideColumn', getFieldsName(item.text,tablename));
				tmenu.menu('setIcon', {
					target: item.target,
					iconCls: 'icon-empty'
				});
			} else {
				$('#'+tablename).datagrid('showColumn', getFieldsName(item.text,tablename));
				tmenu.menu('setIcon', {
					target: item.target,
					iconCls: 'icon-ok'
				});
			}
		}
	});
}
/**
 * 默认标题右键菜单
 * @param e
 * @param field
 * @param tablename
 * @return
 */
function default_context_menu(e, field,tablename){
	e.preventDefault();
	//alert(e.target.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.innerHTML);
	if (!$('#tmenu').length){
		createColumnMenu(tablename);
	}
	$('#tmenu').menu('show', {
		left:e.pageX,
		top:e.pageY
	});
}

/**
 * 得到grid查询参数信息
 * [{field:'',editor:''}]
 * @param grid_id
 * @return
 */
function getQueryArgsInfo(grid_id){		
	var fields = $('#'+grid_id).datagrid('options').columns;
	var frozen_field=$('#'+grid_id).datagrid('options').frozenColumns;
	var field_data=[];
	var finnally_data=[];//最张处理结果
	//普通列
	for(var i=0;i<fields.length;i++){
		for(var j=0;j<fields[i].length;j++){
			if(!fields[i][j].checkbox && "undefined" != typeof fields[i][j].editor){
			field_data.push(fields[i][j]);			
			}
			
		}
	}
	//固定冻结列
	for(var i=0;i<frozen_field.length;i++){
		for(var j=0;j<frozen_field[i].length;j++){
			if(!frozen_field[i][j].checkbox && "undefined" != typeof frozen_field[i][j].editor){
			field_data.push(frozen_field[i][j]);			
			}
			
		}
	}	
	for(var i=0;i<field_data.length;i++){		
		var temp = new Object();
		temp.filed=field_data[i].field;
		temp.editor=field_data[i].editor.type;
		finnally_data.push(temp);
		
	}	
	return finnally_data;
}

function formatterBit(value, row, index){
	return value ?"<img src=\"res/img/ok.png\"/>":"<img src=\"res/img/cross.png\"/>";
}


/**
 * 还原查询参数
 * @param gridid
 * @return
 */
function revert_query_args(gridid){
	var query=$('#'+gridid).datagrid('options').queryParams;
	for(var i in query){
		if(i=="action"||i=="target"){
			
		}else{
			delete query[i];
			}
	}
	$('#'+gridid).datagrid('reload',query);
}

/**
 * @date 2014-9-25
 * @author kingschan 
 * @description  DIY 非高级查询框下自由组合查询方法
 * */
/**
 * 返回一个新增的查询条件对象
 * @param _logic 逻辑关系符 and or
 * @param _operator 操作关系  等于    不等于    大于    小于     小于等于    大于等于    包含    左包含    右包含    集合
 * @param _table 数据表名
 * @param _field 字段名
 * @param _value 值1
 * @param _value2 值2
 * @param _type type: 字段类型，	1)	string	2)	number	3)	date	4)	select 	5)	datetime	6)	boolean
 * @returns {___condition0}
 * f.push(add_condition("and","","",")","","",""));
 */
function add_condition(_logic,_operator,_table,_field,_value,_value2,_type){
	var condition = new Object();
	condition.logic=_logic;//逻辑关系符 and or
	condition.operator=_operator;//操作关系  等于    不等于    大于    小于     小于等于    大于等于    包含    左包含    右包含    集合
	condition.table=_table;//数据表名
	condition.field=_field;//字段名
	condition.value=_value;//值1
	condition.value2=_value2;//值2
	condition.type=_type;//	type: 字段类型，	1)	string	2)	number	3)	date	4)		datetime	5)	boolean
	return condition;
}
/**
 * 执行条件查询
 * @param gridId  表格ID
 * @param filters	条件集合
 * @return
 */
function fun_query(gridId,filters){	
	var current_args=$('#'+gridId).datagrid('options').queryParams;
	current_args.filter=encodeURI(Serialize(filters));		
	$('#'+gridId).datagrid('reload',current_args);
}